package com.itheima.module.web.listener;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

//@WebListener
public class MyServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //System.out.println("项目对象一创建 我就被执行了!!!!!");
        //创建 负责创建service对象和dao对象的那个容器




        XmlWebApplicationContext context = new XmlWebApplicationContext();
        //servletContext 也是可以写配置属性的
        ServletContext servletContext = sce.getServletContext();

        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");

        context.setConfigLocation(contextConfigLocation);

        context.refresh();

        //我要当爸爸去了
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,context);

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
